import {Module} from '../framework.js'
import DataStorage from "../data-storage.js"
import * as PageEvent from "../page/page-event.js"
import PropertyEvent from "./property-event.js"
import * as UI from '../ui.js'

const ProperyMode = Object.freeze({
    MODE_CREATE: 'mode_create',
    MODE_MODIFY: 'mode_modify',
})

export default class Property extends Module {
    constructor(disp) {
        super(disp);
        this._mode = undefined;
        this._PID = undefined;
        this._PSUID = undefined;
        this._arrTag = [];
        this._uiTagGroup = UI.UITagGroup(true);

        const _ui = this._ui = UI.UIProperty();
        _ui.SetTagGroup(this._uiTagGroup);


        this._closeFn = _ => {
            _ui.Hide();
        };
        this._confirmFn = _ => {
            console.log("confirm button");
            /*
            const _newMetaData = this.GetProperies();
            DataStorage.UpdatePageProperty(_newMetaData);
            _ui.Hide();

            const _newMetaData = this.GetProperies();
            DataStorage.AddPageProperty(_newMetaData);
            _ui.Hide();
            */
        };
        this._deleteFn = _ => {
            console.log("delete btn");
            _ui.Hide();
            /*
            const _newMetaData = this.GetProperies();
            DataStorage.UpdatePageProperty(_newMetaData);
            _ui.Hide();
            this._event.DispatchEvent(PropertyEvent.EVT_PROPERTY_DELETE_BTN_CLICKED, this._PID);
            */
        };
        this._createFn = _ => {
            //delete;
            console.log("create btn");
        };

        this.Subscribe(PageEvent.EVT_PAGE_CREATE_PAGE_PROPERTY, event => {
            const _meta = DataStorage.GetPageProperty(event.PID);
            this.Update(ProperyMode.MODE_CREATE, _meta);
            this._ui.Show();
        });
        this.Subscribe(PageEvent.EVT_PAGE_MODIFY_PAGE_PROPERTY, event => {
            const _meta = DataStorage.GetPageProperty(event.PID);
            this.Update(ProperyMode.MODE_MODIFY, _meta);
            this._ui.Show();
        });
    }

    /// instance of MetaData;
    Update(mode, meta) {
        this._PID = undefined;
        this._PSUID = undefined;
        this._mode = mode;
        this._uiTagGroup.Remove();
        const _ui = this._ui;
        switch(mode) {
            case ProperyMode.MODE_MODIFY:
                this._PID = meta.SUID;
                this._PSUID = meta.PSUID;
                _ui.SetTitle('Property');
                _ui.SetData(meta.name, meta.mode, meta.createTime, meta.brief, meta.toc, false, meta.published);
                this._uiTagGroup.Add(meta.tags);
                _ui.SetButtonAction(this._closeFn, this._confirmFn, this._deleteFn);
                break;
            case ProperyMode.MODE_CREATE:
            default:
                this._PSUID = meta ? meta.SUID : undefined;
                _ui.SetTitle('Create');
                _ui.SetData();
                _ui.SetButtonAction(this._closeFn, null, null, this._createFn);
        }
    }

    GetProperies() {
        const _ret = {
            PID: this._PID,
            PSUID: this._PSUID,
        };
        this._ui.GetData(_ret);
        return Object.freeze(_ret);
    }
};

